import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_page_counter.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/model/activity/activity_user_model.dart';

class ActivityPlayerController extends BaseRefreshController
    with BaseRefreshPageCounter {
  final int activityId;

  ActivityPlayerController({required this.activityId});
  final _pageSize = 20;
  List<ActivityUserModel> userItems = List<ActivityUserModel>.of([]);
  RxBool isLoadedData = false.obs;
  @override
  Future<IndicatorResult> onRefresh() async {
    resetPage();
    List<ActivityUserModel>? resp =
        await Api.getUserByActivityId(activityId: activityId);
    isLoadedData.value = true;

    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    List<ActivityUserModel>? resp =
        await Api.getUserByActivityId(activityId: activityId);
    isLoadedData.value = true;
    if (resp == null) {
      return IndicatorResult.fail;
    }
    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }
    incPage();

    return IndicatorResult.success;
  }

  void _setList(List<ActivityUserModel>? list) {
    userItems.clear();
    _addList(list);
  }

  void _addList(List<ActivityUserModel>? list) {
    if (null != list && list.isNotEmpty) {
      userItems.addAll(list);
      update();
    }
  }

  ///关注
  Future<bool> focus(int userId, {required bool? attention}) async {
    final ok = await Api.toogleAttentionUser(userId, attention: attention);
    return ok;
  }
}
